<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let p = {
      name: '张三',
      age: 10
    }
    // p.name = "李四"
    // console.log(p.name);
    // console.log(p.age);
    let handler = {
      // 获取属性
      // target:被代理的对象
      // key:被代理对象的属性
      // return:被代理对象的属性值
      get: function (target, key) {
        console.log(target[key]);
        return target[key];
      },
      //设置属性
      // target:被代理的对象
      // key:被代理对象的属性
      // value:被代理对象的属性值
      set: function (target, key, value) {
        if (key == 'age') {
          if (value < 0 || value > 150) {
            console.log("年龄不合法");
          }
        }
        target[key] = value;
      }
    }
    let proxy = new Proxy(p, handler);//操作代理对象
    proxy.name = "李四";
    proxy.age = 22;
    console.log(proxy.name);


  </script>
</body>

</html>